package com.redoor.app.stake.basicdata.controller;

import com.redoor.app.stake.basicdata.commons.BasicDataStatus;
import com.redoor.app.stake.basicdata.model.BaseParkingArea;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseParkingAreaCriteria;
import com.redoor.app.stake.basicdata.model.serviceModel.BaseParkingAreaServiceBean;
import com.redoor.app.stake.basicdata.service.BaseParkingAreaService;
import com.redoor.app.stake.common.model.Page;
import com.redoornetwork.framework.core.response.ResponseResult;
import com.redoornetwork.framework.core.response.ResultGenerator;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* Created by wangjunlei on 2018-02-12 10:08:10.
*/
@RestController
@RequestMapping("/base/parking/areas")
public class BaseParkingAreaController {

    @Autowired
    private BaseParkingAreaService baseParkingAreaService;

    /**
     *  停车区新增
     *
     * @author ZhangMumu
     * @date 2018/3/23 0023 9:30
     * @param
     * @return
     */
    @ApiOperation(value = "停车区新增或更新")
    @RequiresPermissions("base:parkingArea:insert")
    @PostMapping("/insertOrUpdate")
    public ResponseResult add(@RequestBody BaseParkingArea baseParkingArea) {
        try{
            baseParkingAreaService.saveParkarea(baseParkingArea);
            return ResultGenerator.generatorSuccess();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.generatorFail(BasicDataStatus.OTHER_EXCEPTION);
        }
    }

    /**
     *  停车区删除
     *
     * @author ZhangMumu
     * @date 2018/3/23 0023 9:26
     * @param
     * @return
     */
    @ApiOperation(value = "停车区状态删除,传值:uuid='主键'")
    @RequiresPermissions("base:parkingArea:delete")
    @PostMapping("/del")
    public ResponseResult delete(String uuid) {
        if (StringUtils.isBlank(uuid)){
            return ResultGenerator.generatorFail(BasicDataStatus.PARAM_IS_EMPTY) ;
        }
        try{
            int updatedNum = baseParkingAreaService.delParkarea(uuid);
           return ResultGenerator.generatorSuccess(updatedNum);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.generatorFail(BasicDataStatus.OTHER_EXCEPTION);
        }
    }

    @ApiOperation(value = "停车区详情,传值:id='主键'")
    @PostMapping("/detail")
    public ResponseResult detail(String id) {
        if(StringUtils.isBlank(id)){
            return ResultGenerator.generatorFail(BasicDataStatus.PARAM_IS_EMPTY);
        }
        BaseParkingArea baseParkingArea = baseParkingAreaService.parkareaDetail(id);
        return ResultGenerator.generatorSuccess(baseParkingArea);
    }
    
    /**  
     *  停车区列表查询
     *   
     * @author ZhangMumu  
     * @date 2018/3/23 0023 9:58  
     * @param   
     * @return   
     */  
    @ApiOperation(value = "停车区列表查询", notes = "停车区列表查询")
    @RequiresPermissions("base:parkingArea:list")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "偏移量", paramType = "query", dataType = "int", required = true, defaultValue = "20"),
            @ApiImplicitParam(name = "offset", value = "起始记录", paramType = "query", dataType = "int", required = true, defaultValue = "0"),
            @ApiImplicitParam(name = "code", value = "停车区编码", paramType = "query", dataType = "String", required = false, defaultValue = ""),
            @ApiImplicitParam(name = "stationAddress", value = "停车区地址", paramType = "query", dataType = "String", required = false, defaultValue = ""),
            @ApiImplicitParam(name = "name", value = "停车区名称", paramType = "query", dataType = "String", required = false, defaultValue = "")
    })
    public ResponseResult list(BaseParkingAreaCriteria criteria) {
        try{
            List<BaseParkingAreaServiceBean> list = baseParkingAreaService.qryParkingAreaInfoListForPage(criteria);
            Integer total = baseParkingAreaService.qryParkingAreaInfoListForPageCnt(criteria);
            Page pageInfo = new Page();
            pageInfo.setData(list);
            pageInfo.setTotal(total);
            return ResultGenerator.generatorSuccess(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.generatorFail(BasicDataStatus.OTHER_EXCEPTION);
        }
    }

}
